import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { ATaskAdapter } from ".";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { TaskHelper } from "utils/task/TaskHelper";
import { spawnPriority } from "utils/constants/priorityconstant";
import { CreepTaskChecker } from "../TaskCheck/CreepTaskChecker";

export class PowerCreepTaskAdapter extends ATaskAdapter<"pc">
{
    constructor(context:IContextGetter,world:WorldGetter)
    {
        super(context,world);
        
    }
 
    public bindTasks(room: Room, creeptasks: ITaskDeclare["pc"][],customCheck:IConditionFunction): void 
    {
        const IdlePowerPc = room.getIdlePowerCreeps();
        // room.log("info",`${_.map(IdlePowerPc,e=>e.name)}`)
        for(const task of creeptasks)
        {
            if(customCheck(room as Required<Room>,task) == OK )
            {
                room.deleteTaskById(task.id,"pc");
                continue;
            }
            if(ETaskState.Complete == this.checkState(room,task))
            {
                room.deleteTaskById(task.id,"pc");
                continue;
            }
            if(!this.bindTask(room as Required< Room>,task,IdlePowerPc))
            {
                // 绑定失败PC用完了
                // break;
            }
        }
        
    }
 /**
     * 因为任务会绑定很多个爬,所以在爬的执行内检查就会导致重复运算.
     * 所以只能另外单独检查任务是否完成。以及任务的属性更新
     * // 目前只检查任务是否完成。和属性。  暂时不检查任务是否暂停
     * */ 
    protected checkState(room:Room,task:ITaskDeclare["pc"]):ETaskState
    {
        // room.log("info",'检测pc任务',task.data.state,JSON.stringify(task));
        return task.data.state;
    }
 
    /**
     * 多退少补
     * @param task 
     * @param creep 
     */
    private bindTask(room:Required< Room>,task:ITaskDeclare["pc"],pc:PowerCreep[])
    {
        
        if(task.data.pc)
        {
            return true;
        }
        
        for(let i=0;i<pc.length;i++)
        {
            
            if(pc[i].powers[task.data.arg.POWER])
            {
                pc[i].memory.taskid = task.id;
                task.data.pc = pc[i].name;
                pc.splice(i,1);
                return true;
            }
        }
        if(pc.length==0)
        {
            return false;
        }
        return true;
    }
   

}